package com.bosssoft.cloud.wechat.core.router;

import com.bosssoft.cloud.wechat.core.bean.param.portal.WxMsgParam;
import com.bosssoft.cloud.wechat.infrastructure.constant.WxKeywordConsts;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * WxCore router
 * 核心路由器,负责判断请求类型，
 * 路由微信请求到不同请求类型的router,
 * 进而调用回复对应消息的handler
 *
 * @author wcy create on 2021/6/18 14:30 下午
 */
@Component
public class WxCoreRouter {

    private static final String DEFAULT = "Unknown type";

    @Resource
    private EventRouter eventRouter;
    @Resource
    private TextRouter textRouter;


    /**
     * 根据请求param的msgType，route到对应service处理
     *
     * @param param param
     * @return msg包装
     */
    public String route(WxMsgParam param, HttpSession session) {
        // 判断消息类型
        switch (param.getMsgType()) {
            case WxKeywordConsts.MsgType.EVENT:
                return eventRouter.route(param);
            case WxKeywordConsts.MsgType.TEXT:
                return textRouter.route(param, session);
        }
        return DEFAULT;
    }
}
